<?php

namespace app\common\model;

use think\Model;

class GoodsAttribute extends Model
{
    // 表名
    protected $name = 'goods_attribute';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    
    // 追加属性
    protected $append = [
        'type_text',
        'must_text',
        'search_show_text',
        'create_time_text',
        'update_time_text'
    ];
    

    
    public function getTypeList()
    {
        return ['1' => '单选', '2' => '多选', '3' => '文本'];
    }     

    public function getMustList()
    {
        return ['1' => '是', '0' => '否'];
    }     

    public function getSearchShowList()
    {
	    return ['1' => '是', '0' => '否'];
    }     


    public function getTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['type'];
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getMustTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['must'];
        $list = $this->getMustList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSearchShowTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['search_show'];
        $list = $this->getSearchShowList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['create_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['update_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }
	public function category(){
		return $this->belongsTo('goodsCategory', 'category_id', 'id','', 'left')->setEagerlyType(0);
	}

}
